using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using CoachingOrganizer.Logic.Models.Tools.MoodLevel;

namespace CoachingOrganizer.Android.Adapters.Tools.MoodLevel
{
    public class MoodValuesAdapter : BaseAdapter
    {
        private readonly Activity _context;
        private List<MoodValueCacheModel> _items = new List<MoodValueCacheModel>();
        private readonly MoodDayCacheModel _model;

        public MoodValuesAdapter(Activity context, MoodDayCacheModel model)
            : base()
        {
            _context = context;
            _model = model;
            _items = model.Values.ToList();
        }

        public override Java.Lang.Object GetItem(int position)
        {
            return position;
        }

        public override long GetItemId(int position)
        {
            return position;
        }

        public override int Count
        {
            get { return _items.Count; }
        }

        public override View GetView(int position, View convertView, ViewGroup parent)
        {
            View view = _context.LayoutInflater.Inflate(Resource.Layout.MoodValueLayout, parent, false);
            HookModelToView(view, _items[position]);
            return view;
        }

        private void HookModelToView(View view, MoodValueCacheModel moodValueCacheModel)
        {
            TextView moodValueTimeTextView = view.FindViewById<TextView>(Resource.Id.moodValueTimeTextView);
            moodValueTimeTextView.Text = moodValueCacheModel.Time.ToLongTimeString();
            EditText moodValueEditText = view.FindViewById<EditText>(Resource.Id.moodValueEditText);
            if (moodValueCacheModel.Value.HasValue)
                moodValueEditText.Text = moodValueCacheModel.Value.Value.ToString();
            else
                moodValueEditText.Text = string.Empty;
            moodValueEditText.TextChanged += (sender, e) =>
            {
                decimal decimalValue;
                if (decimal.TryParse(moodValueEditText.Text, out decimalValue))
                    moodValueCacheModel.Value = (double)decimalValue;
            };
        }
    }
}